using Amazon.Runtime.Internal;
using Proto;

namespace sgf.server
{
    public abstract class MessageHandler<TBus, T> : IMessageHandler where T : IRequestMsg where TBus : BusActor
    {
        public static readonly NoneResponse NoneResponse = new();

        public static readonly NoneResponse ErrorResponse = new()
        {
            ErrorCode = 1
        };

        public async Task<IResponseMsg> Run(BusActor busActor, IContext contex)
        {
            if (busActor is not TBus)
            {
                return ErrorResponse;
            }

            await Handle((TBus)busActor, (T)contex.Message, contex);
            return NoneResponse;
        }

        public Type GetMessageType()
        {
            return typeof(T);
        }

        protected abstract Task Handle(TBus busActor, T msg, IContext context);
    }
}